package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.XcxUserBindSysUserBo;
import org.dromara.system.domain.bo.XcxUserBo;
import org.dromara.system.domain.vo.XcxUserVo;
import org.dromara.system.service.IXcxUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序用户
 *
 * @author maoningchuan
 * @date 2025-09-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/XcxUser")
public class SysXcxUserController extends BaseController {

    private final IXcxUserService xcxUserService;

    /**
     * 查询小程序用户列表
     */
    @SaCheckPermission("system:XcxUser:list")
    @GetMapping("/list")
    public TableDataInfo<XcxUserVo> list(XcxUserBo bo, PageQuery pageQuery) {
        return xcxUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出小程序用户列表
     */
    @SaCheckPermission("system:XcxUser:export")
    @Log(title = "小程序用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XcxUserBo bo, HttpServletResponse response) {
        List<XcxUserVo> list = xcxUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "小程序用户", XcxUserVo.class, response);
    }

    /**
     * 获取小程序用户详细信息
     *
     * @param xcxUserId 主键
     */
    @SaCheckPermission("system:XcxUser:query")
    @GetMapping("/{xcxUserId}")
    public R<XcxUserVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long xcxUserId) {
        return R.ok(xcxUserService.queryById(xcxUserId));
    }

    /**
     * 新增小程序用户
     */
    @SaCheckPermission("system:XcxUser:add")
    @Log(title = "小程序用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XcxUserBo bo) {
        return toAjax(xcxUserService.insertByBo(bo));
    }

    /**
     * 修改小程序用户
     */
    @SaCheckPermission("system:XcxUser:edit")
    @Log(title = "小程序用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XcxUserBo bo) {
        return toAjax(xcxUserService.updateByBo(bo));
    }


    /**
     * 获取用户信息
     */
    @GetMapping("/getInfo")
    public R<XcxUserVo> getInfo() {
        Long xcxUserId = LoginHelper.getLoginUser().getUserId();
        XcxUserVo xcxUserVo = xcxUserService.getXcxUserInfo(xcxUserId);
        return R.ok(xcxUserVo);
    }

    /**
     * 删除小程序用户
     *
     * @param xcxUserIds 主键串
     */
    @SaCheckPermission("system:XcxUser:remove")
    @Log(title = "小程序用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{xcxUserIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] xcxUserIds) {
        return toAjax(xcxUserService.deleteWithValidByIds(List.of(xcxUserIds), true));
    }


    /**
     * 删除小程序用户
     *
     * @param xcxUserBindSysUserBo 主键串
     */
    @Log(title = "绑定系统用户", businessType = BusinessType.UPDATE)
    @PostMapping("/bindSysUser")
    public R<Void> bindSysUser(@RequestBody XcxUserBindSysUserBo xcxUserBindSysUserBo) {
        return toAjax(xcxUserService.bindSysUser(xcxUserBindSysUserBo));
    }
}
